package com.lagou.edu.controller;

import com.lagou.edu.pojo.Resume;
import com.lagou.edu.service.ResumeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@RequestMapping("/resume")
public class ResumeController {

    @Autowired
    private ResumeService resumeService;

    @RequestMapping("/queryAll")
    public ModelAndView queryAll() throws Exception {
        ModelAndView modelAndView = new ModelAndView();
        List<Resume> resumes = resumeService.queryAll();
        modelAndView.addObject("resumes", resumes);
        modelAndView.setViewName("list");
        return modelAndView;
    }

    @RequestMapping("/save")
    @ResponseBody
    public String save(Resume resume) throws Exception {
        resumeService.save(resume);
        return "success";
    }

    @RequestMapping("/delete")
    @ResponseBody
    public String delete(long id) throws Exception {
        resumeService.delete(id);
        return "success";
    }

    @RequestMapping("/update")
    @ResponseBody
    public String update(Resume resume) throws Exception {
        resumeService.update(resume);
        return "success";
    }

    @RequestMapping("/getResume")
    public ModelAndView getResume(Long id) throws Exception {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("save");
        if(id==null){
            return modelAndView;
        }
        modelAndView.addObject("resumes", resumeService.getResume(id));

        return modelAndView;
    }

}
